package com.stock.demo.function.home.view;

import android.content.Context;
import android.content.Intent;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.stock.demo.R;
import com.stock.demo.framework.ui.activity.BaseNetActivity;
import com.stock.demo.util.CommonUtils;

/**
 * Created by xuzhaoyu on 17-7-31.
 */

public class CourseDetailActivity extends BaseNetActivity implements CourseDetailView{

    private ImageView mIvFinish;
    private ImageView mIvCollect;
    private ImageView mIvShare;
    private ImageView mIvPlay;
    private TextView mTvPlay;
    private TextView mTvIntroduce;
    private TextView mTvChapter;
    private TextView mTvDiscuss;
    private CourseIntroduceFragment mCourseIntroduceFragment;
    private CourseChapterFragment mCourseChapterFragment;
    private CourseDiscussFragment mCourseDiscussFragment;
    private int mCurrentIndex;

    public static void startAction(Context context) {
        Intent intent = new Intent(context, CourseDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void initView() {
        mIvFinish = (ImageView) findViewById(R.id.iv_course_detail_finish);
        mIvCollect = (ImageView) findViewById(R.id.iv_course_detail_collect);
        mIvShare = (ImageView) findViewById(R.id.iv_course_detail_share);
        mIvPlay = (ImageView) findViewById(R.id.iv_course_detail_play);
        mTvPlay = (TextView) findViewById(R.id.tv_course_detail_play);
        mTvIntroduce = (TextView) findViewById(R.id.tv_course_detail_introduce);
        mTvChapter = (TextView) findViewById(R.id.tv_course_detail_chapter);
        mTvDiscuss = (TextView) findViewById(R.id.tv_course_detail_discuss);
        mIvFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mIvCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了收藏");
            }
        });
        mIvShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了分享");
            }
        });
        mIvPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了播放按钮");
            }
        });
        mTvPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CommonUtils.showToast("点击了播放按钮");
            }
        });
        mTvIntroduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = 0;
                changePage();
            }
        });
        mTvChapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = 1;
                changePage();
            }
        });
        mTvDiscuss.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mCurrentIndex = 2;
                changePage();
            }
        });
    }

    private void changePage() {
        mTvIntroduce.setBackgroundColor(getResources().getColor(R.color.transparent));
        mTvIntroduce.setTextColor(getResources().getColor(R.color.colorPrimary));
        mTvChapter.setBackgroundColor(getResources().getColor(R.color.transparent));
        mTvChapter.setTextColor(getResources().getColor(R.color.colorPrimary));
        mTvDiscuss.setBackgroundColor(getResources().getColor(R.color.transparent));
        mTvDiscuss.setTextColor(getResources().getColor(R.color.colorPrimary));
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction()
                .hide(mCourseIntroduceFragment)
                .hide(mCourseChapterFragment)
                .hide(mCourseDiscussFragment);
        if (mCurrentIndex == 0) {
            mTvIntroduce.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mTvIntroduce.setTextColor(getResources().getColor(R.color.colorWhite));
            transaction.show(mCourseIntroduceFragment);
        } else if (mCurrentIndex == 1) {
            mTvChapter.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mTvChapter.setTextColor(getResources().getColor(R.color.colorWhite));
            transaction.show(mCourseChapterFragment);
        } else if (mCurrentIndex == 2) {
            mTvDiscuss.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            mTvDiscuss.setTextColor(getResources().getColor(R.color.colorWhite));
            transaction.show(mCourseDiscussFragment);
        }
        transaction.commit();
    }

    @Override
    public void initData() {
        onNetNext(new Object()).setNetComplete();
        mCourseIntroduceFragment = new CourseIntroduceFragment();
        mCourseChapterFragment = new CourseChapterFragment();
        mCourseDiscussFragment = new CourseDiscussFragment();
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_course_detail_fragment_container, mCourseIntroduceFragment)
                .add(R.id.fl_course_detail_fragment_container, mCourseChapterFragment)
                .add(R.id.fl_course_detail_fragment_container, mCourseDiscussFragment)
                .hide(mCourseChapterFragment)
                .hide(mCourseDiscussFragment)
                .show(mCourseIntroduceFragment)
                .commit();
    }

    @Override
    public View onCreateSuccessView() {
        return LayoutInflater.from(this).inflate(R.layout.activity_course_learning_detail, null);
    }

    @Override
    public void onReload(View v) {

    }
}
